home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 9 / Example 9.3 / mapObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  3.6 KB  |  116 lines

  1. #include "mapObject.h"
  2. #include "unit.h"
  3. //Line interface to draw selected units/buildings with
  4. ID3DXLine *line = NULL;
  5.  
  6. void LoadMapObjectResources(IDirect3DDevice9* Device)
  7. {
  8.     D3DXCreateLine(Device, &line);
  9. }
  10.  
  11. void UnloadMapObjectResources()
  12. {
  13.     if(line)line->Release();
  14.     line = NULL;
  15. }
  16.  
  17. INTPOINT GetScreenPos(D3DXVECTOR3 pos, IDirect3DDevice9* Device)
  18. {
  19.     D3DXVECTOR3 screenPos;
  20.     D3DVIEWPORT9 Viewport;
  21.     D3DXMATRIX Projection, View, World;
  22.  
  23.     Device->GetViewport(&Viewport);
  24.     Device->GetTransform(D3DTS_VIEW, &View);
  25.     Device->GetTransform(D3DTS_PROJECTION, &Projection);
  26.     D3DXMatrixIdentity(&World);
  27.     D3DXVec3Project(&screenPos, &pos, &Viewport, &Projection, &View, &World);
  28.  
  29.     return INTPOINT(screenPos.x, screenPos.y);
  30. }
  31.  
  32.  
  33. //////////////////////////////////////////////////////////////////////////////////
  34. //                                MapObject                                        //
  35. //////////////////////////////////////////////////////////////////////////////////
  36.  
  37. MAPOBJECT::MAPOBJECT()
  38. {
  39.     //Sets all variables to 0, NULL or False
  40.     m_isBuilding = false;
  41.     m_pTerrain = NULL;
  42.     m_hp = m_hpMax = 0;
  43.     m_range = m_damage = 0;
  44.     m_sightRadius = 0.0f;
  45.     m_team = m_type = 0;
  46.     m_selected = m_dead = false;
  47.     m_pTarget = NULL;
  48.     m_pDevice = NULL;
  49. }
  50.  
  51. RECT MAPOBJECT::GetMapRect(int border)
  52. {
  53.     RECT mr = {m_mappos.x - border, 
  54.                m_mappos.y - border,
  55.                m_mappos.x + m_mapsize.x + border,
  56.                m_mappos.y + m_mapsize.y + border};
  57.  
  58.     return mr;
  59. }
  60.  
  61. void MAPOBJECT::PaintSelected()
  62. {
  63.     if(!m_selected || m_pDevice == NULL)return;
  64.  
  65.     BBOX bbox = GetBoundingBox();    //Bounding box in world space
  66.  
  67.     // Create 8 points according to the corners of the bounding box
  68.     D3DXVECTOR3 corners[] = {D3DXVECTOR3(bbox.max.x, bbox.max.y, bbox.max.z), 
  69.                              D3DXVECTOR3(bbox.max.x, bbox.max.y, bbox.min.z),
  70.                              D3DXVECTOR3(bbox.max.x, bbox.min.y, bbox.max.z),
  71.                              D3DXVECTOR3(bbox.max.x, bbox.min.y, bbox.min.z), 
  72.                              D3DXVECTOR3(bbox.min.x, bbox.max.y, bbox.max.z), 
  73.                              D3DXVECTOR3(bbox.min.x, bbox.max.y, bbox.min.z), 
  74.                              D3DXVECTOR3(bbox.min.x, bbox.min.y, bbox.max.z), 
  75.                              D3DXVECTOR3(bbox.min.x, bbox.min.y, bbox.min.z)};
  76.  
  77.     // Find the max and min points of these
  78.     // 8 offests points in screen space
  79.     INTPOINT pmax(-10000, -10000), pmin(10000,10000);
  80.  
  81.     for(int i=0;i<8;i++)
  82.     {
  83.         INTPOINT screenPos = GetScreenPos(corners[i], m_pDevice);
  84.  
  85.         if(screenPos.x > pmax.x)pmax.x = screenPos.x;
  86.         if(screenPos.y > pmax.y)pmax.y = screenPos.y;
  87.         if(screenPos.x < pmin.x)pmin.x = screenPos.x;
  88.         if(screenPos.y < pmin.y)pmin.y = screenPos.y;        
  89.     }
  90.  
  91.     RECT scr = {-20, -20, 820, 620};
  92.  
  93.     // Check that the max and min point is within our viewport boundaries
  94.     if(pmax.inRect(scr) || pmin.inRect(scr))
  95.     {
  96.         float s = (pmax.x - pmin.x) / 3.0f;
  97.         if((pmax.y - pmin.y) < (pmax.x - pmin.x))s = (pmax.y - pmin.y) / 3.0f;
  98.  
  99.         D3DXVECTOR2 corner1[] = {D3DXVECTOR2(pmin.x, pmin.y + s), D3DXVECTOR2(pmin.x, pmin.y), D3DXVECTOR2(pmin.x + s, pmin.y)};
  100.         D3DXVECTOR2 corner2[] = {D3DXVECTOR2(pmax.x - s, pmin.y), D3DXVECTOR2(pmax.x, pmin.y), D3DXVECTOR2(pmax.x, pmin.y + s)};
  101.         D3DXVECTOR2 corner3[] = {D3DXVECTOR2(pmax.x, pmax.y - s), D3DXVECTOR2(pmax.x, pmax.y), D3DXVECTOR2(pmax.x - s, pmax.y)};
  102.         D3DXVECTOR2 corner4[] = {D3DXVECTOR2(pmin.x + s, pmax.y), D3DXVECTOR2(pmin.x, pmax.y), D3DXVECTOR2(pmin.x, pmax.y - s)};
  103.  
  104.         //Draw the 4 corners
  105.         if(line != NULL)
  106.         {
  107.             line->SetWidth(2.0f);
  108.             line->Begin();
  109.             line->Draw(corner1, 3, 0xffffffff); 
  110.             line->Draw(corner2, 3, 0xffffffff); 
  111.             line->Draw(corner3, 3, 0xffffffff); 
  112.             line->Draw(corner4, 3, 0xffffffff); 
  113.             line->End();
  114.         }
  115.     }
  116. }